/*
 * Created by JFormDesigner on Fri Nov 22 14:32:04 CST 2024
 */

package com.hafu.view.RuKu;

import com.hafu.model.RuKuMain;
import com.hafu.service.RuKuService;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;

/**
 * @author aa
 */
public class QueryRuKuFrm extends JFrame {
    private RuKuService ruKuService = new RuKuService();
    public QueryRuKuFrm() {

        initComponents();
        DefaultTableModel defaultTableModel = (DefaultTableModel) table1.getModel();
        defaultTableModel.setRowCount(0);
        ArrayList<RuKuMain> ruKulist = ruKuService.getRuKuList(new RuKuMain());
        for (RuKuMain ruKu : ruKulist) {
            Object[] rowData = {ruKu.getId(),ruKu.getRukuName(),ruKu.getPrice(),ruKu.getGysName(),ruKu.getRkDate()};
            defaultTableModel.addRow(rowData);
        }
    }

    private void button1(ActionEvent e) {
        // TODO add your code here
        String text = textField1.getText();
        DefaultTableModel defaultTableModel = (DefaultTableModel) table1.getModel();
        defaultTableModel.setRowCount(0);
        RuKuMain ruKuMain = new RuKuMain();
        ruKuMain.setRukuName(text);
        ArrayList<RuKuMain> ruKulist = ruKuService.getRuKuList(ruKuMain);
        for (RuKuMain ruKu : ruKulist) {
            Object[] rowData = {ruKu.getId(),ruKu.getRukuName(),ruKu.getPrice(),ruKu.getGysName(),ruKu.getRkDate()};
            defaultTableModel.addRow(rowData);
        }

    }

    private void table1MouseClicked(MouseEvent e) {
        // TODO add your code here 获取选中行
        int row = table1.getSelectedRow();
        DefaultTableModel dtn = (DefaultTableModel) table1.getModel();
        int chooseId = (int) dtn.getValueAt(row, 0);
        String name = (String) dtn.getValueAt(row, 1);
        textField2.setText(name);

    }

    private void button2(ActionEvent e) {
        // TODO add your code here 修改事件
        String name = textField2.getText();
        int row = table1.getSelectedRow();
        DefaultTableModel dtn = (DefaultTableModel) table1.getModel();
        int chooseId = (int) dtn.getValueAt(row, 0);
        RuKuMain ruKuMain = new RuKuMain();
        ruKuMain.setId(chooseId);
        ruKuMain.setRukuName(name);
        Boolean aBoolean = ruKuService.updateRuKu(ruKuMain);
        if (aBoolean){
            JOptionPane.showMessageDialog(null,"修改成功");
        }else {
            JOptionPane.showMessageDialog(null,"修改失败");
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        textField1 = new JTextField();
        button1 = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        label2 = new JLabel();
        textField2 = new JTextField();
        button2 = new JButton();

        //======== this ========
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u5165\u5e93\u5355\u540d\u5b57");

        //---- button1 ----
        button1.setText("\u786e\u5b9a");
        button1.addActionListener(e -> button1(e));

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                },
                new String[] {
                    "\u7f16\u53f7", "\u540d\u5b57", "\u603b\u4ef7", "\u4f9b\u5e94\u5546\u540d\u5b57", "\u4e0b\u5355\u65f6\u95f4"
                }
            ));
            table1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    table1MouseClicked(e);
                }
            });
            scrollPane1.setViewportView(table1);
        }

        //---- label2 ----
        label2.setText("\u5165\u5e93\u5355\u540d\u5b57");

        //---- button2 ----
        button2.setText("\u4fee\u6539");
        button2.addActionListener(e -> button2(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(65, 65, 65)
                            .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(48, 48, 48)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addGroup(contentPaneLayout.createSequentialGroup()
                                    .addComponent(label2)
                                    .addGap(35, 35, 35)
                                    .addComponent(textField2, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
                                    .addGap(140, 140, 140)
                                    .addComponent(button2))
                                .addGroup(contentPaneLayout.createSequentialGroup()
                                    .addComponent(label1)
                                    .addGap(32, 32, 32)
                                    .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE)
                                    .addGap(137, 137, 137)
                                    .addComponent(button1)))))
                    .addContainerGap(70, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(23, 23, 23)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label1)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(button1))
                    .addGap(41, 41, 41)
                    .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 214, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 59, Short.MAX_VALUE)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label2)
                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(button2))
                    .addGap(44, 44, 44))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JTextField textField1;
    private JButton button1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JLabel label2;
    private JTextField textField2;
    private JButton button2;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
